<template>
  <div class="top-list">
    <transition name="slider">
      <music-list :title="title" :bg-image="bgImg" :songs="songs" :rank="rank"/>
    </transition>
  </div>
</template>

<script>
    import musicList from "@/components/music-list/music-list";
    import {mapGetters} from "vuex";
    import {getMusicList} from "../../api/rank";
    import {ERR_OK} from "../../api/config";
    import {createdSong} from "../../common/js/song";

    export default {
        name: "top-list",
        data() {
          return {
            songs: [],
            rank: true
          }
        },
        computed: {
          ...mapGetters([
            'toplist'
          ]),
          title() {
            return this.toplist.topTitle;
          },
          bgImg() {
            if (this.songs.length) {
              return this.songs[0].image;
            }
            return '';
          },
        },
        created() {
          this._getMusicList();
        },
        methods: {
          _getMusicList() {
            if (!this.toplist.id) {
              this.$router.push("/rank");
              return;
            }
            getMusicList(this.toplist.id).then(res => {
              if (res.code === ERR_OK) {
                this.songs = this._normalizeSongs(res.songlist);

              }

            })
          },
          _normalizeSongs(list) {
            let ret = [];
            list.forEach((item) => {
              const muiscData = item.data;
              if (muiscData.songid && muiscData.albumid) {
                ret.push(createdSong(muiscData))
              }
            });
            return ret;
          }
        },
        components: {
          musicList
        }
    }
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
  .slide-enter-active, .slide-leave-active
    transition: all 0.3s ease

  .slide-enter, .slide-leave-to
    transform: translate3d(100%, 0, 0)
</style>
